package com.example.administrator.pandanotes;

import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import org.litepal.crud.DataSupport;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by 于博 on 2018-07-14.
 * 笔记添加界面
 * 有添加按钮，点击添加笔记
 */
public class NoteAddActivity extends AppCompatActivity {
    //变量***************************************************************************
    private Button btnAdd = null;
    private EditText eTBookName = null;
    private EditText eTNoteCon = null;
    private EditText eTPageNum = null;
    private Date date = null;
    private Note note = new Note();
    User theUser = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_add);
        //初始化及绑定****************************************************************
        eTBookName = (EditText)findViewById(R.id.eTAddBookName);
        eTNoteCon = (EditText)findViewById(R.id.eTAddNoteCon);
        eTPageNum = (EditText)findViewById(R.id.eTAddPageNumber);
        btnAdd = (Button)findViewById(R.id.btnAddNote);
        try {
            FileReader r = new FileReader(Environment.getExternalStorageDirectory() + "/pandanotescookies.txt");
            BufferedReader reader = new BufferedReader(r);
            String id = reader.readLine();
            Log.d("UserName:",id);
            if(id != null && (!id.equals(""))){
                List<User> users = DataSupport.where("user_id = ?",id).find(User.class);
                theUser = users.get(0);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //函数或事件*****************************************************************
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//添加按钮点击事件
                boolean flag = true;
                //获得值
                String BookName = eTBookName.getText().toString();
                String PageNum = eTPageNum.getText().toString();
                String Note = eTNoteCon.getText().toString();
                List<Books> books = DataSupport.where("book_Name=?",BookName).find(Books.class);//找到用户所填书籍名
                //异常处理
                if (books.size()==0)
                {
                    flag=false;
                    Toast.makeText(NoteAddActivity.this,"不存在此书籍",Toast.LENGTH_SHORT).show();
                }
                else if (PageNum.length()==0)
                {
                    flag=false;
                    Toast.makeText(NoteAddActivity.this,"页数不能为空",Toast.LENGTH_SHORT).show();
                }
                else if (PageNum.length()>4)
                {
                    flag=false;
                    Toast.makeText(NoteAddActivity.this,"页数过大",Toast.LENGTH_SHORT).show();
                }
                else if (!isNumeric(PageNum))
                {
                    flag=false;
                    Toast.makeText(NoteAddActivity.this,"页数应为数字",Toast.LENGTH_SHORT).show();
                }
                else if (isNumeric(PageNum))
                {
                    flag=false;
                    int i = Integer.valueOf(PageNum);
                    if (i <= 0)
                        Toast.makeText(NoteAddActivity.this, "页数应大于0", Toast.LENGTH_SHORT).show();
                    else
                        flag=true;
                }
                if (Note.length()==0)
                {
                    flag=false;
                    Toast.makeText(NoteAddActivity.this,"笔记不能为空",Toast.LENGTH_SHORT).show();
                }
                if (flag)
                {
                    SimpleDateFormat    formatter    =   new    SimpleDateFormat    ("yyyy年MM月dd日    HH:mm:ss     ");
                    date = new Date(System.currentTimeMillis());//获取当前时间
                    String    str    =    formatter.format(date);
                    Log.d("书籍名和笔记:",BookName+Note);
                    Log.d("时间:",str);
                    note.setStr_date(str);
                    note.setBook_name(BookName);
                    note.setNote_content(Note);
                    note.setNote_page(PageNum);
                    Books book = books.get(0);
                    book.getNotes().add(note);//但愿加到了书籍里面
                    note.setBooks(book);
                    boolean b = note.save();//保存
                    Log.d("b",b+"");
                    if(b)Toast.makeText(NoteAddActivity.this, "添加成功！", Toast.LENGTH_SHORT).show();
                    else Toast.makeText(NoteAddActivity.this, "添加失败！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(NoteAddActivity.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        });
    }
    //判断字符串是否由数字组成函数，使用ASCLL码
    public static boolean isNumeric(String str){
        for(int i=str.length();--i>=0;){
            int chr=str.charAt(i);
            if(chr<48 || chr>57)
                return false;
        }
        return true;
    }
    //返回键调用函数，进行上一页*************************************************************
    @Override
    public void onBackPressed()
    {
        this.finish(); //关闭当前Activity
    }
}
